<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>云邮蜂巢 - 用户登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', 'PingFang SC', sans-serif;
        }
        
        body {
            height: 100vh;
            display: flex;
            background-color: #f5f5f5;
        }
        
        .login-section {
            width: 50%;
            position: relative;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #fff;
        }
        
        .top-bar {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 10%;
            border-bottom: 1px solid #eaeaea;
            position: absolute;
            top: 0;
            left: 0;
            background-color: white;
            z-index: 10;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
            color: #ff9500;
            display: flex;
            align-items: center;
        }
        
        .logo i {
            margin-right: 10px;
            font-size: 28px;
        }
        
        .login-container {
            width: 100%;
            max-width: 400px;
            margin: 120px auto 0;
            padding: 0 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        .hero-image {
            width: 50%;
            background: linear-gradient(135deg, #ff9500, #ff5000);
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
        }
        
        .hero-image::before {
            content: "";
            position: absolute;
            width: 100%;
            height: 100%;
            background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="%23ffffff22"><path d="M30,50 Q50,30 70,50 T90,50" stroke="none"/></svg>');
            background-size: 100px 100px;
            opacity: 0.6;
        }
        
        .login-title {
            text-align: center;
            font-size: 24px;
            color: #333;
            margin-bottom: 30px;
            width: 100%;
            font-weight: bold;
        }
        
        .input-group {
            margin-bottom: 20px;
            width: 100%;
            position: relative;
        }
        
        .input-label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
            text-align: left;
        }
        
        .input-field {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 15px;
            background-color: #fff;
            transition: all 0.3s;
        }
        
        .input-field:focus {
            border-color: #ff9500;
            outline: none;
            box-shadow: 0 0 5px rgba(255, 149, 0, 0.3);
        }
        
        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(90deg, #ff9500, #ff5000);
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            margin-top: 10px;
            letter-spacing: 1px;
            transition: all 0.3s;
        }
        
        .login-btn:hover {
            background: linear-gradient(90deg, #ff8500, #ff4000);
            box-shadow: 0 2px 10px rgba(255, 85, 0, 0.3);
        }
        
        .login-btn.disabled {
            background: #ccc;
            cursor: not-allowed;
            box-shadow: none;
        }
        
        .bottom-links {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
            width: 100%;
        }
        
        .link {
            color: #999;
            text-decoration: none;
            transition: color 0.3s;
        }
        
        .link:hover {
            color: #ff9500;
        }
        
        .agreement-container {
            display: flex;
            align-items: center;
            margin: 15px 0;
            width: 100%;
        }
        
        .agreement-text {
            font-size: 14px;
            color: #999;
        }
        
        .agreement-link {
            color: #ff9500;
            text-decoration: none;
            cursor: pointer;
            margin: 0 3px;
        }
        
        .agreement-link:hover {
            text-decoration: underline;
        }
        
        .other-login {
            margin-top: 40px;
            width: 100%;
            text-align: center;
        }
        
        .other-login-title {
            color: #999;
            font-size: 14px;
            position: relative;
            margin-bottom: 20px;
        }
        
        .other-login-title::before,
        .other-login-title::after {
            content: "";
            position: absolute;
            top: 50%;
            width: 30%;
            height: 1px;
            background-color: #eee;
        }
        
        .other-login-title::before {
            left: 0;
        }
        
        .other-login-title::after {
            right: 0;
        }
        
        .login-icons {
            display: flex;
            justify-content: center;
            gap: 20px;
        }
        
        .login-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: #f5f5f5;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s;
            color: #666;
            font-size: 18px;
        }
        
        .login-icon:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .login-icon.wechat {
            color: #07C160;
        }
        
        .login-icon.qq {
            color: #12B7F5;
        }
        
        .login-icon.weibo {
            color: #E6162D;
        }
        
        .login-icon.github {
            color: #333;
        }
        
        .error-message {
            color: #ff4d4f;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }
        
        .hero-content {
            color: white;
            text-align: center;
            padding: 20px;
            z-index: 1;
        }
        
        .hero-content h2 {
            font-size: 28px;
            margin-bottom: 15px;
        }
        
        .hero-content p {
            font-size: 16px;
            margin-bottom: 10px;
        }
        
        @media (max-width: 1024px) {
            .login-section {
                width: 60%;
            }
            
            .hero-image {
                width: 40%;
            }
        }
        
        @media (max-width: 768px) {
            body {
                flex-direction: column;
            }
            
            .login-section, .hero-image {
                width: 100%;
            }
            
            .login-container {
                margin-top: 80px;
                padding: 0 20px;
            }
            
            .hero-image {
                height: 200px;
                padding: 40px 0;
            }
        }
    </style>
</head>
<body>
    <div class="login-section">
        <div class="top-bar">
            <div class="logo">
                <i class="fas fa-mail-bulk"></i>
                <span>云邮蜂巢</span>
            </div>
            <div style="display: flex; align-items: center;">
                <a href="#" style="color: #666; text-decoration: none; font-size: 14px;">返回首页</a>
            </div>
        </div>
        
        <div class="login-container">
            <form id="loginForm" style="width: 100%;">
                <h1 class="login-title">欢迎登录云邮蜂巢</h1>
                
                <div class="input-group">
                    <label for="email" class="input-label">邮箱或用户名</label>
                    <input type="text" id="emailOrUsername" class="input-field" placeholder="请输入邮箱或用户名" required>
                    <div id="email-error" class="error-message">请输入邮箱或用户名</div>
                </div>

                <div class="input-group">
                    <label for="password" class="input-label">密码</label>
                    <input type="password" id="password" class="input-field" placeholder="请输入密码" required>
                    <div id="password-error" class="error-message">请输入密码</div>
                </div>
                
                <div class="agreement-container">
                    <label class="agreement-text">
                        我已阅读并同意
                        <a href="#" class="agreement-link">用户服务协议</a>
                        和
                        <a href="#" class="agreement-link">隐私协议</a>
                    </label>
                </div>
                
                <button type="submit" class="login-btn" id="submitBtn">登 录</button>
                
                <div class="bottom-links">
                    <a href="register.html" class="link">立即注册</a>
                    <a href="forget.html" class="link">忘记密码?</a>
                </div>
                
                <div class="other-login">
                    <div class="other-login-title">其他登录方式</div>
                    <div class="login-icons">
                        <div class="login-icon wechat" title="微信登录"><i class="fab fa-weixin"></i></div>
                        <div class="login-icon qq" title="QQ登录"><i class="fab fa-qq"></i></div>
                        <div class="login-icon weibo" title="微博登录"><i class="fab fa-weibo"></i></div>
                        <div class="login-icon github" title="GitHub登录"><i class="fab fa-github"></i></div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    
    <div class="hero-image">
        <div class="hero-content">
            <h2>云邮蜂巢</h2>
            <p>高效稳定的企业邮箱服务</p>
            <p>安全可靠 · 智能管理 · 极速收发</p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const loginForm = document.getElementById('loginForm');
            const submitBtn = document.getElementById('submitBtn');
            const emailOrUsernameInput = document.getElementById('emailOrUsername');
            const passwordInput = document.getElementById('password');
            const emailError = document.getElementById('email-error');
            const passwordError = document.getElementById('password-error');
            
            // 显示错误信息
            function showError(element, message) {
                element.textContent = message;
                element.style.display = 'block';
            }
            
            // 隐藏错误信息
            function hideError(element) {
                element.style.display = 'none';
            }
            
            // 验证表单
            function validateForm() {
                let isValid = true;
                
                // 验证邮箱或用户名
                if (!emailOrUsernameInput.value.trim()) {
                    showError(emailError, '请输入邮箱或用户名');
                    isValid = false;
                } else {
                    hideError(emailError);
                }
                
                // 验证密码
                if (!passwordInput.value) {
                    showError(passwordError, '请输入密码');
                    isValid = false;
                } else {
                    hideError(passwordError);
                }
                
                return isValid;
            }
            
            // 表单提交验证
            loginForm.addEventListener('submit', async function(e) {
                e.preventDefault();
                
                if (!validateForm()) {
                    return;
                }
                
                submitBtn.disabled = true;
                submitBtn.classList.add('disabled');
                submitBtn.textContent = '登录中...';
                
                try {
                    // 准备登录数据
                    const loginData = {
                        password: passwordInput.value
                    };
                    
                    // 判断是邮箱还是用户名
                    const inputValue = emailOrUsernameInput.value.trim();
                    if (inputValue.includes('@')) {
                        loginData.email = inputValue;
                    } else {
                        loginData.userID = inputValue;
                    }
                    
                    // 发送登录请求
                    const response = await fetch("/api/v1/user/login", {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(loginData)
                    });
                    
                    if (!response.ok) {
                        const errorData = await response.json();
                        throw new Error(errorData.message || '登录失败');
                    }
                    
                    const result = await response.json();
                    
                    // 存储返回的数据到本地存储
                    localStorage.setItem('token', result.token);
                    localStorage.setItem('role', result.role);
                    localStorage.setItem('userId', result.userId);
                    
                    // 登录成功跳转
                    window.location.href = 'index.html';
                    
                } catch (error) {
                    console.error('登录错误:', error);
                    alert(error.message || '登录失败，请检查输入信息');
                    submitBtn.disabled = false;
                    submitBtn.classList.remove('disabled');
                    submitBtn.textContent = '登 录';
                }
            });
            
            // 输入时实时验证
            emailOrUsernameInput.addEventListener('input', function() {
                if (this.value.trim()) {
                    hideError(emailError);
                }
            });
            
            passwordInput.addEventListener('input', function() {
                if (this.value) {
                    hideError(passwordError);
                }
            });
        });
    </script>
</body>
</html>